<?php
/**
 * Extend Zend_Application_Resource_Translate
 * Auto select translate file by locale
 * 
 * @copyright  Copyright (c) 2011 Ricky Feng (http://code.google.com/p/rphp4zf)
 * @license    New BSD License
 */

class Mediad2_Application_Resource_Multiftp extends Zend_Application_Resource_ResourceAbstract
{
	/**
     * Associative array containing all configured server's
     *
     * @var array
     */
    protected $_ftps = array();

    /**
     * An instance of the default ftp server, if set
     */
    protected $_defaultFtp;
	
    /**
     * init
     */
    public function init()
    {
    	$options = $this->getOptions();
    	
    	foreach ($options as $id => $params) {
    		$default = (int) (isset($params['default']) && $params['default']);
    		unset($params['default']);
    	
    		$this->_ftps[$id] = $params;
    	
    		if ($default) {
    			$this->_defaultFtp = $this->_ftps[$id];
    			//保存进注册表
    			Zend_Registry::set('defaultFtp', $this->_defaultFtp);
    		}
    		
    		//保存进注册表
    		Zend_Registry::set($params['registryName'], $params);
    	}

    	return $this;
    }
    
    /**
     * isDefault
     * @param string $ftpname
     * @return boolean
     */
    public function isDefault($ftpname)
    {
    	$ftp = $this->getFtp($ftpname);
    	return $ftp === $this->_defaultFtp;
    }
    
    /**
     * getFtp
     * @param string $ftpname
     * @throws Zend_Application_Resource_Exception
     */
    public function getFtp($ftpname = null)
    {
    	if ($ftpname === null) {
    		return $this->getDefault();
    	}
    
    	if (isset($this->_ftps[$ftpname])) {
    		return $this->_ftps[$ftpname];
    	}
    
    	throw new Zend_Application_Resource_Exception(
    			'A Ftp server was tried to retrieve, but was not configured'
    	);
    }
    
    /**
     * getDefault
     * 
     * @return NULL|array
     */
    public function getDefault()
    {
    	if ($this->_defaultFtp !== null) {
    		return $this->_defaultFtp;
    	}
    	return null;
    }

}

